﻿using LD.Host;
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.BC
{
    public partial class AxisMove : Form
    {
        public AxisMove()
        {
            InitializeComponent();
            this.Shown += AxisMove_Shown;
            this.lab_axisPosition.Text = "0";
            this.lab_currentVal.Text = "0";
        }

        private void AxisMove_Shown(object sender, EventArgs e)
        {
            //BcFactory.Instance.MonitorningEvent += Monitor_Monitoring_Event;
        }

        //private void Monitor_Monitoring_Event(MonitoringData m_Data)
        //{
        //    this.Invoke(new Action(() =>
        //    {
        //        this.txt_axisPosition.Text = string.Format("{0:N3}", m_Data.Position[AxisId]);
        //        //this.txt_axisPosition.Text = Math.Round(m_Data.Position[AxisId], 4) + "";
        //    }));
        //}

        public ushort AxisId { get; set; }
        private string axisName;
        public string AxisName
        {
            get { return axisName; }
            set
            {
                axisName = value;
                this.lab_name.Text = value;
            }
        }
        private void btn_goHome_Click(object sender, EventArgs e)
        {

        }

        private void btn_stop_Click(object sender, EventArgs e)
        {

        }

        private void btn_jog__MouseDown(object sender, MouseEventArgs e)
        {
            Task.Run(new Action(() =>
            {
                this.Invoke(new Action(() =>
                {
                    btn_jog_.BackColor = Color.Cyan;
                }));
            }));
            BcFactory.Instance.Control.Move(AxisId, (ushort)MoveDir.反方向);
        }

        private void btn_jog__MouseUp(object sender, MouseEventArgs e)
        {
            Task.Run(new Action(() =>
            {
                this.Invoke(new Action(() =>
                {
                    btn_jog_.BackColor = SystemColors.Control;
                }));
            }));
            BcFactory.Instance.Control.Stop(AxisId);
        }

        private void btn_jog_MouseUp(object sender, MouseEventArgs e)
        {
            Task.Run(new Action(() =>
            {
                this.Invoke(new Action(() =>
                {
                    btn_jog.BackColor = SystemColors.Control;
                }));
            }));
            BcFactory.Instance.Control.Stop(AxisId);
        }

        private void btn_jog_MouseDown(object sender, MouseEventArgs e)
        {
            Task.Run(new Action(() =>
            {
                this.Invoke(new Action(() =>
                {
                    btn_jog.BackColor = Color.Cyan;
                }));
            }));
            BcFactory.Instance.Control.Move(AxisId, (ushort)MoveDir.正方向);
        }

        private void btn_move__Click(object sender, EventArgs e)
        {

        }

        private void btn_move_Click(object sender, EventArgs e)
        {

        }
        public void M_Closed()
        {
            //BcFactory.Instance.MonitorningEvent -= Monitor_Monitoring_Event;
            this.Close();
        }
        private void AxisMove_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void btn_clearAlert_Click(object sender, EventArgs e)
        {

        }

        private void btn_sensor_Click(object sender, EventArgs e)
        {

        }

        private void btn_goHome_Click_1(object sender, EventArgs e)
        {
            Task.Run(new Action(() =>
            {
                BcFactory.Instance.Control.Move_BackZero(AxisId);
            }));
        }

        private void btn_stop_Click_1(object sender, EventArgs e)
        {
            Task.Run(new Action(() =>
            {
                BcFactory.Instance.Control.Stop(AxisId);
            }));
        }

        private void btn_jog__Click(object sender, EventArgs e)
        {

        }

        private void btn_jog_Click(object sender, EventArgs e)
        {

        }

        private void btn_move__Click_1(object sender, EventArgs e)
        {
            double moveSize = (double)num_moveSize.Value;
            //if (!double.TryParse(.Validate, out moveSize))
            //{
            //    Dialog dialog = new Dialog();
            //    dialog.Warning("定量数据格式错误");
            //    return;
            //}
            Task.Run(new Action(() =>
            {
                BcFactory.Instance.Control.Move_Ration(AxisId, (ushort)MoveDir.反方向, moveSize);
            }));
        }

        private void btn_move_Click_1(object sender, EventArgs e)
        {
            double moveSize = (double)num_moveSize.Value;

            Task.Run(new Action(() =>
            {
                BcFactory.Instance.Control.Move_Ration(AxisId, (ushort)MoveDir.正方向, moveSize);
            }));
        }

        private void btn_clearAlert_Click_1(object sender, EventArgs e)
        {
            Task.Run(new Action(() =>
            {
                BcFactory.Instance.Control.ClearAlart(AxisId);
            }));
        }

        private void btn_sensor_Click_1(object sender, EventArgs e)
        {
            Task.Run(new Action(() =>
            {
                BcFactory.Instance.Control.SetSevon(AxisId, true);
            }));
        }

        private void btn_jog_1_Click(object sender, EventArgs e)
        {

        }

        private void btn_jog1_Click(object sender, EventArgs e)
        {

        }
    }
}
